package com.it.schoolbookshop_back.controller;

import com.it.schoolbookshop_back.entities.dto.LoginDTO;
import com.it.schoolbookshop_back.entities.dto.LoginResponseDTO;
import com.it.schoolbookshop_back.entities.dto.RegisterDTO;
import com.it.schoolbookshop_back.entities.po.Result;
import com.it.schoolbookshop_back.entities.po.User;
import com.it.schoolbookshop_back.service.UserService;
import com.it.schoolbookshop_back.utils.JwtUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param loginDTO 登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result<LoginResponseDTO> login(@RequestBody LoginDTO loginDTO) {
        // 参数校验
        if (loginDTO.getUsername() == null || loginDTO.getPassword() == null) {
            return Result.validateFailed("用户名或密码不能为空");
        }
        
        // 调用服务层进行登录验证
        User user = userService.login(loginDTO.getUsername(), loginDTO.getPassword());
        
        // 登录失败
        if (user == null) {
            return Result.validateFailed("用户名或密码错误");
        }
        
        // 生成JWT令牌，包含用户角色信息
        String token = JwtUtil.generateToken(user.getId().toString(), user.getUsername(), user.getRole());
        
        // 返回token和用户角色
        LoginResponseDTO responseDTO = new LoginResponseDTO(token, user.getRole());
        return Result.success("登录成功", responseDTO);
    }

    /**
     * 用户注册
     * @param registerDTO 注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result<Integer> register(@RequestBody RegisterDTO registerDTO) {
        // 参数校验
        if (registerDTO.getUsername() == null || registerDTO.getPassword() == null || registerDTO.getReal_name() == null) {
            return Result.validateFailed("用户名、姓名和密码不能为空");
        }
        
        // 检查用户名是否已存在
        if (userService.isUsernameExists(registerDTO.getUsername())) {
            return Result.validateFailed("该用户名已被注册");
        }
        
        // 组装用户对象
        User user = new User();
        user.setUsername(registerDTO.getUsername());
        user.setPassword(registerDTO.getPassword());
        user.setStudent_id(registerDTO.getStudent_id());
        user.setReal_name(registerDTO.getReal_name());
        user.setDepartment(registerDTO.getDepartment());
        user.setEmail(registerDTO.getEmail());
        user.setPhone(registerDTO.getPhone());
        user.setWechat(registerDTO.getWechat());
        user.setBio(registerDTO.getBio());
        
        // 调用服务层进行注册
        Integer userId = userService.register(user);
        
        // 注册失败
        if (userId == null) {
            return Result.failed("注册失败，请稍后再试");
        }
        
        // 返回用户ID
        return Result.success("注册成功", userId);
    }
} 